<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class SystemController extends BaseController
{
    //主页
    public function dictionary(){

        if (request()->isPost()){
            writelog("查看了数据字典");
            $table_name=input("param.table_name");
            $datebase=input("param.datebase");
            $data=Db::query("SELECT
	  TABLE_SCHEMA as datebase,
    TABLE_NAME as  table_name,
    COLUMN_NAME as table_z,
    COLUMN_TYPE as table_type,
    COLUMN_COMMENT as table_zname
FROM
    information_schema. COLUMNS
WHERE
    TABLE_SCHEMA='x_admin' 
AND TABLE_SCHEMA='{$datebase}' AND TABLE_NAME='{$table_name}'");

            $result['code'] = 1;
            $result['msg'] = "";
            $result['data'] = $data;
            return json($result);
        }
        $keyword=input("param.keyword");
        if (!empty($keyword)){
            if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$keyword)) {
                //中文
                $data=Db::query("SELECT * FROM (
SELECT
	  TABLE_SCHEMA as datebase,
    TABLE_NAME as  table_name,
    COLUMN_NAME,
    COLUMN_TYPE as table_type,
    COLUMN_COMMENT as table_zname
FROM
    information_schema. COLUMNS
WHERE
    TABLE_SCHEMA='x_admin'
GROUP BY TABLE_NAME) aa WHERE aa.table_zname like '%{$keyword}%'");
            } else {
                //英文
                $data=Db::query("SELECT * FROM (
SELECT
	  TABLE_SCHEMA as datebase,
    TABLE_NAME as  table_name,
    COLUMN_NAME,
    COLUMN_TYPE as table_type,
    COLUMN_COMMENT as table_zname
FROM
    information_schema. COLUMNS
WHERE
    TABLE_SCHEMA='x_admin'
GROUP BY TABLE_NAME) aa WHERE aa.table_name like '%{$keyword}%'");
            }
            $this->assign('data',$data);// 赋值
            $this->assign('keyword',$keyword);// 赋值

        }else{

            $data=Db::query("SELECT
	  TABLE_SCHEMA as datebase,
    TABLE_NAME as  table_name,
    COLUMN_NAME,
    COLUMN_TYPE as table_type,
    COLUMN_COMMENT as table_zname
FROM
    information_schema. COLUMNS
WHERE
    TABLE_SCHEMA='x_admin'
GROUP BY TABLE_NAME");
            $this->assign('data',$data);// 赋值
            $this->assign('keyword',$keyword);// 赋值
        }

        return $this->fetch("dictionary");
    }

    /*
     * 日志列表
     */
    public function log(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $sum=($page-1)*$limit;
            $keyword=input("param.keyword");
            $where=[];
            if ($keyword){
                $where["a.names"]=["like","%{$keyword}%"];
            }
            $f[]="la.*";
            $f[]="a.names as admin_name";
            $data=Db::name("log_admin")
                ->alias('la')
                ->join("admin a"," a.id=la.admin_id","left")
                ->where($where)
                ->limit("$sum,$limit")
                ->field($f)
                ->select();
            $num=Db::name("log_admin")
                ->alias('la')
                ->join("admin a"," a.id=la.admin_id","left")
                ->where($where)
                ->field($f)
                ->select();
            $result['code'] = 0;
            $result['msg'] = "";
            $result['count'] = count($num);
            $result['data'] = $data;
            return json($result);
        }

        return $this->fetch("log");

    }

    /*
     * 删除日志
     */
    public function log_del(){
        $id=input("param.id");
        $ids=input("ids/a");
        if ($ids){
            $ids=implode(",",$ids);
            $result=Db::name("log_admin")->where("id in ($ids)")->delete();

        }else{
            $result=Db::name("log_admin")->where(["id"=>$id])->delete();
        }

        if ($result){
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }

    /*
     * 登录日志
     */
    public function login_admin(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $sum=($page-1)*$limit;
            $keyword=input("param.keyword");
            $where=[];
            if ($keyword){
                $where["a.names"]=["like","%{$keyword}%"];
            }
            $f[]="la.*";
            $f[]="a.names as admin_name";
            $data=Db::name("login_admin")
                ->alias('la')
                ->join("admin a"," a.id=la.admin_id","left")
                ->where($where)
                ->limit("$sum,$limit")
                ->field($f)
                ->select();
            $num=Db::name("login_admin")
                ->alias('la')
                ->join("admin a"," a.id=la.admin_id","left")
                ->where($where)
                ->field($f)
                ->select();
            $result['code'] = 0;
            $result['msg'] = "";
            $result['count'] = count($num);
            $result['data'] = $data;
            return json($result);
        }

        return $this->fetch("login_admin");
    }

}